//index.js
//获取应用实例
var app = getApp();
var WxParse = require('../../untils/wxParse/wxParse.js')
Page({
  data: {
    previewImg: '',
    pageCount: '',
    pageno: 1,
    renderObj: {
      listArr: []
    },
    bottomTxt: '',
    showComment: false,
    showCommentIndex: '',
    leaveWords: '',
    showPublic: false,
    publicContent: '',
    defaultAdd: '浙江省',
    msgid: '',
    userInfo: '',
    reCommShowFlag: true,
    currentCommid: '',
    commentContent: '',
    msgid: '',
    picServer: app.globalData.picServer,
    reloadFlag: false,
    perviewShow: true,
    perviewImg: '',
  },
  pick: function (e) {
    var nam = [];
    var num = e.currentTarget.dataset.id;
    this.setData({
      'leadIndex': nam
    })
  },
  onReady: function () {
    this.hiDialog = this.selectComponent('#hiDialog');
    this.msgDialog = this.selectComponent('#msgDialog');
  },
  onShow() {
    var _this = this;
    var userid = app.globalData.userid;
    if (!userid) {
      var openid = wx.getStorageSync('openid');
      if (!openid) {
        app.wxLogin().then(function (v) {//
          if (v) {
            app.ifLogin().then(function () {
              _this.initPage({});
            })
          }
        })
      } else {
        app.getUserInfoSyn().then(function () {
          _this.initPage({});
        })
      }
    } else {
      _this.initPage({});
    }
    if (this.data.reloadFlag) {
      _this.initPage({});
    }
  },
  onLoad: function (opt) {
    console.log(opt);
  },
  initPage: function (options) {
    wx.showToast({
      title: '加载中',
      icon: 'loading',
      mask: true,
    })
    var _this = this;
    var addName = wx.getStorageSync('addName');
    var addCode = wx.getStorageSync("addCode") ? wx.getStorageSync("addCode") : "33";
    _this.data.defaultAdd = addName || _this.data.defaultAdd;
    app.doReq(app.globalData.SERVICE.INDEX.MYMOOD, {
      "id": app.globalData.userid,
      "pagesize": 12,
      "pageno": _this.data.pageno,
      "code": addCode
    }, 'GET', function (res) {
      wx.hideLoading();
      var data = res.data;
      if (data.code == 200) {
        if (_this.data.pageno == 1) {
          _this.setData({
            renderObj: {
              listArr: data.pageitems,
            },
            pageCount: data.pagecount,
          });
        } else {
          _this.setData({
            renderObj: {
              listArr: _this.data.renderObj.listArr.concat(data.pageitems)
            },
          });
        }
      }
    });
  },
  onReachBottom: function () {//刷新
    console.log('下拉了')
    var _this = this;
    var currentPageNo = _this.data.pageno;
    if (currentPageNo <= _this.data.pageCount) {
      _this.data.pageno++;
      _this.initPage({});
    }
  },
  doComment: function (e) {
    //记录上次点击的对象的序号
    var oldidx = this.data.currentCommid;
    this.data.msgid = e.currentTarget.dataset.msgid;
    //记录当前点击的对象的序号
    var currentidx = e.currentTarget.dataset.idx;
    var chooseFlag = this.data.reCommShowFlag;
    if (oldidx == currentidx) {
      this.setData({
        reCommShowFlag: !chooseFlag
      });
    } else {
      this.setData({
        currentCommid: currentidx,
        reCommShowFlag: chooseFlag
      });
    }
  },
  doSaveComment(e) {
    var _this = this;
    let msgid = this.data.msgid;
    if (msgid == undefined) {
      msgid = ''
    }
    app.doReq(app.globalData.SERVICE.INDEX.MOODMSGSAVE, {
      "id": app.globalData.userid,
      'dynamicMsg.content': _this.data.commentContent,
      'dynamicMsg.msg.id': msgid,
      "dynamicMsg.dyId": e.currentTarget.dataset.dyid
    }, 'get', function (res) {
      let data = res.data;
      if (data.code == 200) {
        wx.showToast({
          title: '发送成功',
          icon: 'success'
        });
        _this.initPage({});
        _this.setData({
          reCommShowFlag: false,
          commentContent: '',
        })
        // alert("发送成功~")
        // app.init();
        // app.leaveWords = '';
      }
    });
  },
  whenInputComment: function (e) {
    var content = e.detail.value;
    this.data.commentContent = content;
  },
  sendMsg() {
    this.msgDialog.show();
  },
  goMemberDetail: function (e) {
    var userid = e.currentTarget.dataset.userid;
    wx.navigateTo({
      url: '../../pages/memberDetails/memberDetails?userid=' + userid
    });
  },
  goPush: function () {
    wx.navigateTo({
      url: '../../pages/pesonalMoodAdd/pesonalMoodAdd'
    })
  },
  hidePerview: function () {
    this.setData({
      perviewShow: true,
      perviewImg: '',
    })
  },
  doPerview: function (e) {
    var url = e.currentTarget.dataset.url;
    this.setData({
      perviewShow: false,
      perviewImg: url,
    })
  },
  doDel:function(e){
    var _this=this;
    var id = e.currentTarget.dataset.id
    app.confirm("提示","确认删除?").then(function(v){
          if(v){
            app.doReq(app.globalData.SERVICE.USERCENTER.MOOD_DEL,{
              id:id,
            },'get',function(res){
              if (res.code == 200) {
                alert("删除成功！");
              }
              _this.initPage({});
            })
          }
    });
    
  }
});